// pages/manage/manage.js
var verify = require('../../../utils/verify-token.js');
var app = getApp()
var ONLINE = "false";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    size:10,
    loading:true,
    filterValue:"",
    isFresh:false,
    inputShowed: true,
  },  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var size = this.data.size
    var token = app.globalData.token;
    var filterValue = this.data.filterValue
    var that = this
    var netStatus = 'ALL';   
    ONLINE = options.online;    
    if (ONLINE=="true"){
      netStatus = "在线";
    }
    wx.request({
      url: app.globalData.baseUrl + 'devices/pagedSearch?productId=ALL&extendProperty=ALL&networkType=ALL&specs=ALL&netStatus='+netStatus+'&deviceStatus=ALL&filter=identifier&page=0&size=' + size + '&filterValue=' + filterValue,
      header: {
        'AUTHORIZATION': token
      },
      success: function (res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          // console.log("deviceList = ",res)
          var deviceList = res.data.data.content
          var totalElements = res.data.data.totalElements
          var loading = false
          if (totalElements > size) {
            loading = true
          }
          that.setData({
            deviceList: deviceList,
            totalElements: totalElements,
            loading: loading,
            isFresh: false
          })
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
      }
    })


    this.setData({
      sh: app.globalData.screenHeight,
      sw: app.globalData.screenWidth
    })
  },
// 搜索相关
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  // 取消搜索
  hideInput: function () {
    this.setData({
      inputShowed: false,
      filterValue: '',
      inputVal: ''
    });
    this.search();
  },
  inputTyping: function (e) {
    this.setData({
      filterValue: e.detail.value
    });
    this.search();
  },
  // 搜索框输入
  input: function (res) {
    this.setData({
      filterValue: res.detail.value
    })
  },
  search:function(){
    var filterValue = this.data.filterValue
    var size = 10
    this.setData({
      filterValue: filterValue,
      size: size
    })
    this.onLoad({online:ONLINE});
  },

  // 查询设备详情
  deviceDetail:function(res){
	// console.log(res)
    var deviceid = res.currentTarget.dataset.deviceid;
	var productId = res.currentTarget.dataset.productid;
	var productType = res.currentTarget.dataset.producttype;
    wx.navigateTo({
      url: 'deviceDetail/deviceDetail?deviceid='+deviceid+'&productId='+productId+'&productType='+productType,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var size = this.data.size*1+10
    var loading = this.data.loading
    var totalElements = this.data.totalElements
    this.setData({
      isFresh: true
    })
    if (size-10 > totalElements){
      this.setData({
        loading:false
      })
    }else{
      this.setData({
        size: size
      })
      this.onLoad({online:ONLINE})
    }
    
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})